﻿/// <reference path = "../CellTypes/Base.ts" />
namespace VantCommands {
    interface ToastParam {
        message?: string;
        type?: string;
        duration?: number;
        position?: string;
        icon?: VantCellTypes.Icon;
        iconSize: number;
        loadingType: "spinner" |"circular";
        overlay?: boolean;
        forbidClick?: boolean;
        closeOnClick?: boolean;
        closeOnClickOverlay?: boolean;
    }

    export class ToastMessage extends Forguncy.Plugin.CommandBase {

        static unclickableClassName = "van-toast--unclickable";

        public async execute(): Promise<void> {
            const param = <ToastParam>this.CommandParam;

            if (param.forbidClick) {
                $("body").addClass(ToastMessage.unclickableClassName);
            }

            const onClose = () => {
                $("body").removeClass(ToastMessage.unclickableClassName);
            };

            const toastData = {
                message: this.evaluateFormula(param.message)?.toString(),
                type: param.type,
                duration: param.duration ?? 0,
                position: param.position,
                overlay: !!param.overlay,
                iconSize: param.iconSize,
                loadingType: param.loadingType,
                forbidClick: !!param.forbidClick,
                closeOnClick: !!param.closeOnClick,
                closeOnClickOverlay: !!param.closeOnClickOverlay,
                icon: null,
                onClose
            };

            return new Promise((resolve) => {
                if (param.icon?.Name && param.type !== "loading") {
                    VantCellTypes.IconHelper.getIcon(param.icon, icon => {
                        toastData.icon = icon;
                        window.vant.Toast(toastData);
                        resolve();
                    });
                }
                else {
                    window.vant.Toast(toastData);
                    resolve();
                }
            });
        }
    }
}

Forguncy.Plugin.CommandFactory.registerCommand("Vant.Commands.Toast, Vant", VantCommands.ToastMessage);